using System.Net;
using System.Net.Sockets;
using SuperSocket.Client;

namespace KestrelClientWithSuperSocket;

internal sealed class EachClient
{
    private IEasyClient<EachPackage, EachPackage> _client =
        new EasyClient<EachPackage, EachPackage>(new EachPipeLineFilter(), new EachPackageEncode()).AsClient();

    public EachClient()
    {
        _client.Closed += OnClosed;
        _client.PackageHandler += OnPackageHandler;
    }
    
    public async ValueTask StartAsync()
    {
        Console.WriteLine("连接中");

        var connected = await _client.ConnectAsync(new DnsEndPoint("127.0.0.1", 5006, AddressFamily.InterNetwork));

        Console.WriteLine($"连接结果: {connected}");

        _client.StartReceive();
    }
    
    void OnClosed(object? sender, EventArgs e)
    {
        Console.WriteLine("断开连接");
    }

    ValueTask OnPackageHandler(IEasyClient<EachPackage>? client, EachPackage packet)
    {
        Console.WriteLine($"content：{packet.Content}");

        return ValueTask.CompletedTask;
    }
}